<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
class Section
{
	private $db;
	private $table;
	private $tableCate;
	private $tableData;
	
	public function __construct()
	{
		$this->db=new Mysql();
		$this->table = $this->db->pre.'section';
		$this->tableCate = $this->db->pre.'section_cate';
		$this->tableData = $this->db->pre.'section_data';
	}
	
	public function getSection($id=null)
	{
		$scatid = Http::get('scatid');
		$keys = Http::get('keys');		
		$name = Http::get('name');
		
		$sql = "SELECT a.*,b.cname FROM ".$this->table." a 		
		LEFT JOIN ".$this->tableCate." b ON a.scatid = b.id
		WHERE 1 ";
		if ($id) {
			$sql=$sql." AND a.id = $id ";
			return $this->db->getRow($sql);
		}
		
		if (!empty($scatid)){
			$_GET['cname'] = $this->db->getOne("SELECT cname FROM ".$this->tableCate." WHERE id = '$scatid'");
			$sql = $sql." AND a.scatid = $scatid ";
		}
		if (!empty($keys)){			
			$sql = $sql." AND a.keys  like '%".$keys."%' ";
		}
		if (!empty($name)){			
			$sql = $sql." AND a.name  like '%".$name."%' ";
		}
		
		//echo $sql;
		$page = new Page();
		$page->orderBy = 'a.sorts';
		$page->desc = ' ASC ';
		$page->uriStyle ='/';
		$list = $page->getPage($sql);
		$html = $page->htmlPage();
		return array('list'=>$list,'html'=>$html);
	}
	public function add($data)
	{
		
		return $this->db->insert($this->table, $data);
	}
	public function update($sectionId,$data){
		return $this->db->updateData($this->table, $data,"WHERE id = $sectionId ");
	}
	public function del($id){
	
		$sql = "UPDATE ".$this->tableData." SET sectionid = 0 WHERE sectionid in( $id ) ";
		$this->db->update($sql);
		$sql = "DELETE FROM ".$this->table."  WHERE id in ( $id )";
		return $this->db->delete($sql);
		
		
	}
	
	public function getCate($cateId=null){
		if ($cateId){
			$sql = "SELECT * FROM ".$this->tableCate." WHERE id = $cateId  ";
			return $this->db->getRow($sql);
		}else{
			$sql = "SELECT * FROM ".$this->tableCate." WHERE 1 ORDER BY `sorts` ASC ";
			return $this->db->getAll($sql);
		}
	}
	public function getCateList(){
		$sql = "SELECT * FROM ".$this->tableCate." WHERE 1 ";
		$page = new Page();
		$page->orderBy = 'sorts';
		$page->desc = 'ASC';
		$list = $page->getPage($sql);
		$html = $page->htmlPage();
		return array('list'=>$list,'html'=>$html);		
	}
	public function addCate($name,$sort)
	{
		$data = array('cname'=>$name,'sorts'=>$sort);
		return $this->db->insert($this->tableCate, $data);
	}
	public function updateCate($id,$name,$sort)
	{
		$sql = "UPDATE ".$this->tableCate." SET `cname` = '$name',`sorts`= '$sort' WHERE id = $id LIMIT 1";
		return $this->db->update($sql);
	}
	public function delCate($id)
	{	
		$sql = "DELETE FROM ".$this->tableCate." WHERE id = ".$id;
		return $this->db->delete($sql);
		
	}
	public function getSectiondata($sectionId=null)
	{
		$page = new Page();
		$page->orderBy = 'sorts';
		$page->desc = 'ASC';
		
		$sql = "SELECT a.*,b.name FROM ".$this->tableData." a LEFT JOIN ".$this->table." b ON a.sectionid = b.id WHERE 1 ";
		if (!empty($sectionId)){
			$sectionInfo = self::getSection($sectionId);
			$sql = $sql." AND sectionid = '$sectionId'";
			$page->eachNums = $sectionInfo['nums'];	
		}	
			
		$list = $page->getPage($sql);
		$html = $page->htmlPage();
		return array('list'=>$list,'html'=>$html);
		
		
		return $this->db->getAll($sql);
	}
	public function getSectiondataRow($id)
	{
		$sql = "SELECT * FROM ".$this->tableData." WHERE id = '$id' ";
		
		return $this->db->getRow($sql);
	}
	public function addSectiondata($data){
		$data['ctime']=time();
		return $this->db->insert($this->tableData, $data);
	}
	public function updateSectiondata($id,$data)
	{
		$data['ctime']=time();
		return $this->db->updateData($this->tableData, $data, " WHERE id = '$id'");
	}
	public function delData($id)
	{
		$sql = "DELETE FROM ".$this->tableData." WHERE id IN( ".$id." )";
		return $this->db->delete($sql);
	}
}